#include <stdlib.h>
#include <string.h>
#include "stdio.h"
#include "sqlite3.h"
#include "database.h"
#include "stock.h"

// 修改库存
int stock_update(Stock1 *S){
    if (S == NULL) {
        fprintf(stderr, "stock_update: 传入的库存指针为空！\n");
        return -1;
    }
    char sql[256];
    char *errmsg=NULL;
    sprintf(sql,"update create_stock set food_name='%s',current_quantity=%d where stock_id=%d;",
            S->food_name,S->current_quantity,S->stock_id);
    int rc= sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"stock_update:修改库存失败：%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("stock_update:stock_id=%d 的库存修改成功！\n", S->stock_id);
    return 0;
}
static int callback(void *data, int argc, char **argv, char **arg_col_name) {
    (*(int *)data)++;  // 统计记录数
    static int is_first_row = 1;

    // 第一行打印字段名（仅打印需要的4个字段）
    if (is_first_row) {
        printf("| stock_id（库存ID） | food_id（菜品ID） | food_name（菜品名称） | current_quantity（库存数量） |\n");
        printf("------------------------------------------------------------------------------------\n");
        is_first_row = 0;
    }

    // 按字段顺序提取数据（argv 索引对应 SQL 查询结果的列顺序）
    // stock_id, food_id, food_name, current_quantity
    char *stock_id = argv[0] ? argv[0] : "NULL";
    char *food_id = argv[1] ? argv[1] : "NULL";
    char *food_name = argv[2] ? argv[2] : "NULL";
    char *current_quantity = argv[3] ? argv[3] : "NULL";

    // 格式化打印，对齐字段
    printf("| %-15s | %-13s | %-18s | %-20s |\n",
           stock_id, food_id, food_name, current_quantity);

    return 0;
}
// 查询库存信息
int stock_query(){
    char sql[256];
    int rowcount=0;
    char *errmsg=NULL;
    sprintf(sql, "select stock_id, food_id, food_name, current_quantity from create_stock;");
    int rc= sqlite3_exec(db,sql,callback,&rowcount,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"stock_query:查询失败：%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("stock_query:查询成功！共查询到 %d 条库存数据\n", rowcount);
    return 0;
}
//// 增加库存
//int stock_insert(Stock1 *s){
//    printf("--------------------------------------\n");
//    char sql[256];
//    char *errmsg=NULL;
//    sprintf(sql,"insert into create_stock(food_id,food_name,current_quantity)values(%d,'%s',%d);",
//            s->food_id,s->food_name,s->current_quantity);
////    printf("sql: %s\n",sql);
//    int rc= sqlite3_exec(db,sql,NULL,NULL,&errmsg);
//    if(rc!=SQLITE_OK){
//        fprintf(stderr,"stock_insert插入数据失败：%s\n",errmsg);
//        sqlite3_free(errmsg);
//        return -1;
//    }
//    printf("插入数据成功！\n");
//    return 0;
//}
#define FOOD_COUNT 18
const char * foods[FOOD_COUNT][6] = {
        {"1",   "原味羊肉串", "6.5", "1", "精选羊腿肉，炭火烤制，撒少许盐","1"},
        {"2",   "秘制牛肉串", "7.0", "1", "牛里脊腌制8小时，微辣",        "2"},
        {"3",   "奥尔良鸡翅", "8.0", "1", "鸡翅中，奥尔良酱腌制，甜香",    "3"},
        {"4",   "炭烤生蚝",   "12.0","2", "鲜活生蚝，蒜蓉粉丝打底",       "4"},
        {"5",   "烤鱿鱼须",   "9.0", "2", "深海鱿鱼须，刷秘制辣酱",       "5"},
        {"6",   "烤茄子",     "10.0","3", "整只茄子剖开，加蒜蓉小米辣",    "6"},
        {"7",   "烤金针菇",   "6.0", "3", "金针菇卷培根，刷蜂蜜",         "7"},
        {"8",   "烤馒头片",   "3.0", "4", "刀切馒头切片，烤至金黄，刷炼乳", "8"},
        {"9",   "锡纸娃娃菜", "12.0","4", "娃娃菜加高汤，粉丝，酸辣味",   "9"},
        {"10",  "烤羊腰",    "15.0", "5", "原油羊腰，炭火慢烤，去腥增香",  "10"},
        {"11", "烤鸡胗",     "5.0",  "5", "鸡胗切花，腌制后烤至脆嫩",      "11"},
        {"12", "烤猪脑",     "18.0", "5", "鲜猪脑，锡纸包裹，加泡椒蒜蓉",   "12"},
        {"13", "烤土豆片",   "4.0", "6", "东北特色土豆，撒辣椒面孜然",      "13"},
        {"14", "烤豆皮卷",   "3.0", "6", "干豆皮卷香菜，刷甜酱",           "14"},
        {"15", "烤蚕蛹",     "8.0", "6", "鲜活蚕蛹，炸后烤制，高蛋白",      "15"},
        {"16", "冰镇啤酒",   "6.0", "7", "青岛啤酒", "16"},
        {"17", "酸梅汤",     "5.0", "7", "自制酸梅汤，冰镇解腻", "17"},
        {"18", "可乐",       "4.0", "7", "可口可乐", "18"}
};
int stock_insert() {
    printf("开始批量插入库存数据...\n");
    printf("=====================================\n");

    int success_count = 0;  // 成功插入的记录数
    int fail_count = 0;     // 插入失败的记录数

    // 遍历你的菜品数组，逐个插入库存
    for (int i = 0; i < FOOD_COUNT; i++) {
        Stock1 stock;
        memset(&stock, 0, sizeof(Stock1));

        // 从菜品数组中提取数据（仅取需要的3个字段）
        stock.food_id = atoi(foods[i][0]);  // 数组第1列：food_id（字符串转int）
        strncpy(stock.food_name, foods[i][1], sizeof(stock.food_name)-1);  // 数组第2列：food_name
        stock.current_quantity = 100;  // 自定义初始库存（可修改为任意值，比如50、200）

        // 拼接SQL语句（仅操作3个字段）
        char sql[256];
        sprintf(sql, "insert into create_stock(food_id, food_name, current_quantity) values(%d, '%s', %d);",
                stock.food_id, stock.food_name, stock.current_quantity);

        // 执行SQL
        char *errmsg = NULL;
        int rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        if (rc != SQLITE_OK) {
            fprintf(stderr, "失败：food_id=%d，%s\n", stock.food_id, errmsg);
            sqlite3_free(errmsg);
            fail_count++;
        } else {
            printf("成功：food_id=%d | %s | 初始库存=%d\n",
                   stock.food_id, stock.food_name, stock.current_quantity);
            success_count++;
        }
    }
    // 统计结果
    printf("=====================================\n");
    printf("批量插入完成！\n");
    printf("成功插入：%d 条记录\n", success_count);
    printf("插入失败：%d 条记录\n", fail_count);
    printf("=====================================\n");
    return (fail_count == 0) ? 0 : -1;
}